#tag Class
Protected Class LangContainer
	#tag Method, Flags = &h1
		Protected Sub AppendValue(prefName as string, prefValue as string)
		  if not Exists(prefName) then
		    mPrefNames.Append prefName
		    mPrefValues.Append prefValue
		    mNumPrefs = mNumPrefs + 1
		  end
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub Clear()
		  while NumPrefs() > 0
		    DeleteNth(0)
		  wend
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub Constructor(prefFileName as String)
		  // Written by Steve LoBasso <slobasso@yahoo.com>
		  // Version 1.2
		  mFileName = prefFileName
		  mNumPrefs = 0
		  redim mPrefNames(-1)
		  redim mPrefValues(-1)
		  mScramblePrefix = "."
		  mScrambleKey = "scramble"
		  Clear()
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h1
		Protected Sub DecodePreference(prefEntry as string)
		  dim aPrefEntry as string
		  dim prefName as string
		  dim prefValue as string
		  
		  aPrefEntry = Trim(prefEntry)
		  if aPrefEntry <> "" and Left(aPrefEntry, 1) <> ";" and Left(aPrefEntry, 1) <> "#" and InStr(aPrefEntry, "=") <> 0 then
		    
		    prefName = Trim(NthField(aPrefEntry, "=", 1))
		    prefValue = DefineEncoding(Trim(Mid(aPrefEntry, InStr(aPrefEntry, "=") + 1)),Encodings.UTF8)
		    
		    Put(prefName, prefValue)
		  end
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub Delete(prefName as string)
		  DeleteNth(FindPrefIndex(prefName))
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h1
		Protected Sub DeleteNth(prefIndex as integer)
		  if prefIndex >= 0 and prefIndex < NumPrefs() then
		    mPrefNames.Remove prefIndex
		    mPrefValues.Remove prefIndex
		    mNumPrefs = mNumPrefs - 1
		  end
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h1
		Protected Function EncodePreference(prefIndex as integer) As String
		  dim prefName as string
		  dim prefValue as string
		  
		  prefName = GetNthName(prefIndex)
		  prefValue = GetNthValue(prefIndex,"")
		  
		  return prefName + " = " + prefValue
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function Exists(prefName as string) As Boolean
		  return(FindPrefIndex(prefName) >= 0)
		End Function
	#tag EndMethod

	#tag Method, Flags = &h1
		Protected Function FindPrefIndex(prefName as string) As Integer
		  dim currPref as integer
		  dim foundPref as integer
		  
		  foundPref = -1
		  currPref = 0
		  while foundPref = -1 and currPref < NumPrefs()
		    if Uppercase(GetNthName(currPref)) = Uppercase(prefName) then
		      foundPref = currPref
		    end
		    currPref = currPref + 1
		  wend
		  return(foundPref)
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function Get(prefName as string) As String
		  return(GetNthValue(FindPrefIndex(prefName), prefName))
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetCaption(id as string) As String
		  return(Get(id))
		End Function
	#tag EndMethod

	#tag Method, Flags = &h1
		Protected Function GetNthName(prefIndex as integer) As String
		  dim prefName as string
		  
		  if prefIndex >= 0 and prefIndex < NumPrefs() then
		    prefName = mPrefNames(prefIndex)
		  else
		    prefName = ""
		  end
		  return(prefName)
		End Function
	#tag EndMethod

	#tag Method, Flags = &h1
		Protected Function GetNthValue(prefIndex as integer, prefName as string) As String
		  dim prefValue as string
		  
		  if prefIndex >= 0 and prefIndex < NumPrefs() then
		    prefValue = mPrefValues(prefIndex)
		  else
		    if AddMissingValues then putcaption(prefName,"")
		    prefValue = ""
		  end
		  return(prefValue)
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetPrefFileName() As String
		  return(mFileName)
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub LoadCaptions(w as window)
		  dim i1,i2 as integer
		  dim s as string
		  dim t as Introspection.TypeInfo //for some reason, w.name is empty, RB bug?
		  
		  t=Introspection.GetType(w)
		  
		  w.title=app.lang.getcaption(t.name)
		  
		  for i1=0 to w.ControlCount-1
		    
		    if w.control(i1) IsA StaticText then StaticText(w.control(i1)).Text=app.lang.getcaption(t.name+"."+w.control(i1).name)
		    
		    if w.control(i1) IsA BevelButton then BevelButton(w.control(i1)).caption=app.lang.getcaption(t.name+"."+w.control(i1).name)
		    
		    if w.control(i1) IsA CheckBox then CheckBox(w.control(i1)).caption=app.lang.getcaption(t.name+"."+w.control(i1).name)
		    
		    if w.control(i1) IsA PushButton then PushButton(w.control(i1)).caption=app.lang.getcaption(t.name+"."+w.control(i1).name)
		    
		    if w.control(i1) IsA RadioButton then RadioButton(w.control(i1)).caption=app.lang.getcaption(t.name+"."+w.control(i1).name)
		    
		    if w.control(i1) IsA GroupBox then GroupBox(w.control(i1)).Caption=app.lang.getcaption(t.name+"."+w.control(i1).name)
		    
		    if w.control(i1) IsA TabPanel then
		      
		      s=Tabpanel(w.control(i1)).Name
		      
		      TabPanel(w.control(i1)).Name=app.lang.getcaption(t.name+"."+w.control(i1).name)
		      
		      for i2=0 to TabPanel(w.control(i1)).TabCountMBS-1
		        
		        TabPanel(w.control(i1)).Caption(i2)=app.lang.getcaption(t.name+"."+s+"."+str(i2))
		        
		      next
		      
		    end
		    
		    if w.control(i1) IsA PagePanel then
		      PagePanel(w.control(i1)).Name=app.lang.getcaption(t.name+"."+w.control(i1).name)
		    end
		    
		    
		  next
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub LoadMenuItems(m as menubar)
		  dim s as string
		  dim t as Introspection.TypeInfo //for some reason, m.name is empty, RB bug?
		  t=Introspection.GetType(m)
		  s=t.name
		  
		  if m.count>0 then
		    for i1 as integer=0 to m.count-1
		      
		      m.item(i1).Text=app.lang.getcaption(s+"."+m.item(i1).name)
		      
		    next
		  end
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h1
		Protected Function NumPrefs() As Integer
		  return(mNumPrefs)
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub PrefContainer(prefFileName as String)
		  // Written by Steve LoBasso <slobasso@yahoo.com>
		  // Version 1.2
		  mFileName = prefFileName
		  mNumPrefs = 0
		  redim mPrefNames(-1)
		  redim mPrefValues(-1)
		  mScramblePrefix = "."
		  mScrambleKey = "scramble"
		  Clear()
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h1
		Protected Function PrefLocation() As FolderItem
		  dim f as folderitem
		  dim s as string
		  
		  s=NthField(app.ExecutableFile.name,".",1)
		  
		  f=SpecialFolder.SharedApplicationData.Child(s)
		  if not f.exists then f.CreateAsFolder
		  
		  return(f)
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub Put(prefName as string, prefValue as string)
		  dim prefIndex as integer
		  
		  prefIndex = FindPrefIndex(prefName)
		  if prefIndex >= 0 then
		    SetNthValue(prefIndex, prefValue)
		  else
		    AppendValue(prefName, prefValue)
		  end
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub PutCaption(prefName as string, prefValue as string)
		  Put(prefName, prefValue)
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub Read()
		  Dim inFile As TextInputStream
		  
		  if GetPrefFileName() <> "" then
		    
		    inFile = PrefLocation().Child(GetPrefFileName()).OpenAsTextFile
		    
		    if inFile <> nil then
		      
		      inFile.Encoding=Encodings.UTF8
		      
		      while not inFile.EOF
		        DecodePreference(inFile.ReadLine())
		      wend
		      inFile.Close()
		    end
		  end
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h1
		Protected Sub SetNthValue(prefIndex as integer, prefValue as string)
		  if prefIndex >= 0 and prefIndex < NumPrefs() then
		    mPrefValues(prefIndex) = prefValue
		  end
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub SetPrefFileName(theFileName as string)
		  mFileName = theFileName
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Function Write() As boolean
		  dim prefFile as folderitem
		  dim outFile As TextOutputStream
		  dim prefIndex as integer
		  
		  if GetPrefFileName() <> "" then
		    prefFile = PrefLocation().Child(GetPrefFileName())
		    prefFile.Delete()
		    outFile = prefFile.CreateTextFile
		    if outFile <> nil then
		      prefIndex = 0
		      while prefIndex < NumPrefs()
		        outFile.WriteLine(EncodePreference(prefIndex))
		        prefIndex = prefIndex + 1
		      wend
		      outFile.Close()
		    end
		  end
		  
		  return true
		End Function
	#tag EndMethod


	#tag Note, Name = readme
		*** ENCODINGS ***
		
		If you create/edit language files using Notepad, remember to save to file in UTF8 -format, not ASCII.
	#tag EndNote


	#tag Property, Flags = &h1
		Protected mFileName As string
	#tag EndProperty

	#tag Property, Flags = &h1
		Protected mNumPrefs As integer
	#tag EndProperty

	#tag Property, Flags = &h1
		Protected mPrefNames(0) As string
	#tag EndProperty

	#tag Property, Flags = &h1
		Protected mPrefValues(0) As string
	#tag EndProperty

	#tag Property, Flags = &h1
		Protected mScrambleKey As string
	#tag EndProperty

	#tag Property, Flags = &h1
		Protected mScramblePrefix As string
	#tag EndProperty


	#tag Constant, Name = AddMissingValues, Type = Boolean, Dynamic = False, Default = \"true", Scope = Protected
	#tag EndConstant


	#tag ViewBehavior
		#tag ViewProperty
			Name="Index"
			Visible=true
			Group="ID"
			InitialValue="-2147483648"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Left"
			Visible=true
			Group="Position"
			InitialValue="0"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Name"
			Visible=true
			Group="ID"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Super"
			Visible=true
			Group="ID"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Top"
			Visible=true
			Group="Position"
			InitialValue="0"
			InheritedFrom="Object"
		#tag EndViewProperty
	#tag EndViewBehavior
End Class
#tag EndClass
